package cn.pzhu.javaee.mybook.dao;

import java.util.Date;
import java.util.List;

import cn.pzhu.javaee.mybook.domain.Detail;
import cn.pzhu.javaee.webapp.dao.BaseDao;
/*
id	int(11)	No	
type	 varchar(32)	No	
catogeryId	int(11)	No	
remark	varchar(255)	No	
amount	int(11)	No	
recordDate	date	No	
userId	int(11)	No	
createTime	datetime	No	

 */
public class DetailDao extends BaseDao<Detail> {

	public List<Detail> listByUserId(int userId){
		return this.list("select a.*, b.name as catogeryName "+
				"from detail as a,catogery as b where a.catogeryId=b.id "+
				"and userId=?",userId);
	}
	public List<Detail> pageByUserId(int startIndex,int limit,int userId){
		return this.list("select a.*, b.name as catogeryName "+
				"from detail as a,catogery as b where a.catogeryId=b.id "+
				"and userId=? order by a.id desc limit ?,?",userId,startIndex,limit);
	}
	public long countOfDetailByUserId(int userId){
		return this.getValue("select count(*) from detail where userId=?", userId); 
	}
	public boolean addDetail(Detail d){
		String sql="insert into detail(type,catogeryId,remark,amount,"
				+ "recordDate,userId,createTime)values(?,?,?,?,?,?,?)";
		return this.save(sql, d.getType(),d.getCatogeryId(),d.getRemark(),d.getAmount(),
				d.getRecordDate(),d.getUserId(),d.getCreateTime());
	}
	public Detail findDetailById(int id){
		return this.get("select * from detail where id=?", id);
	}
	
	public boolean updateDetail(int id,String type,int catogeryId,
			Date recordDate,int amount,String remark,int userId){
		String sql="update detail set type=?,catogeryId=?,"
				+ "recordDate=?,amount=?,remark=?,userId=?,createTime=? "
				+ "where id=?";
		return 1==this.update(sql,type,catogeryId,
				recordDate,amount,remark,userId,new Date(),
				id);
		
	}
	
	public boolean delDetailById(int id){
		return 1==this.delete("delete from detail where id=?", id);
	}
	
	
	
}
